//
//  CarPicsViewController.m
//  AutoHome
//
//  Created by wqh on 15-5-22.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "CarPicsViewController.h"
#import "PicFlowLayout.h"
#import "CarPicCell.h"

#define kCarPicCellId @"CarPicCellId"

@interface CarPicsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic) NSMutableArray *dataArr;
@property (nonatomic) UICollectionView *collectionView;
@end

@implementation CarPicsViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor redColor];
    self.dataArr = [[NSMutableArray alloc] init];
    [self createCollectionView];
    [self downloadData];
}

- (void)createCollectionView {
    PicFlowLayout *flowLayout = [[PicFlowLayout alloc] init];
   
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CarPicCell" bundle:nil] forCellWithReuseIdentifier:kCarPicCellId];
    
    [self.view addSubview:self.collectionView];
    
}

- (void)downloadData {
    
    [[DataEngine sharedInstance] requestCarBrandPicDetailWithId:self.pic_id success:^(id respondsData) {
        [self downloadDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
    
}

- (void)downloadDataSuccess:(id)respondsData {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:respondsData options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *picDict in dict[@"result"][@"piclist"]) {
        [self.dataArr addObject:picDict[@"smallpic"]];
    }
    [self.collectionView reloadData];
}

#pragma mark -UICollectionViewDataSourece 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CarPicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCarPicCellId forIndexPath:indexPath];
    NSLog(@"%@",self.dataArr[indexPath.row]);
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.dataArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"default_80_60"]];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
